/*
 *  joingame_window.cpp
 */

#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
#include "client_app.h"
#include "client_window.h"
#include "joingame_window.h"
#include "../../common/src/debug.h"
#include "../../common/src/lock.h"
#include "../../common/src/synchro_vector.h"

JoinGameWindow::JoinGameWindow(BaseObjectType* object,
                               const Glib::RefPtr<Gtk::Builder>& builder)
                              :ClientWindow(object, builder) {
	builder->get_widget("CancelJoinButton", btnCancel);
	btnCancel->signal_clicked().connect(sigc::mem_fun(*this,
                                                    &JoinGameWindow::onCancel));
}

JoinGameWindow::~JoinGameWindow() {}

void JoinGameWindow::setApp(ClientApp* app) {
    this->app = app;
    app->addListener(this);
}

void JoinGameWindow::setLobbyWindow(Gtk::Window* window) {
    winLobby = window;
}


void JoinGameWindow::onStartGame() {
    Lock lock(&mutex);
    app->requestStartGame();
    while (status == STATUS_WAITING) {
        usleep(1);
    }
    if (status == STATUS_OK) {
        PRINT("START_GAME_INVOKED");
        //winLobby->show();
        //get_application()->add_window(*winLobby);
        //get_application()->remove_window(*this);
        //hide();
    }
}

void JoinGameWindow::onCancel() {
    Lock lock(&mutex);
    app->requestAbortGame();
    status = STATUS_WAITING;
    while (status == STATUS_WAITING) {
        usleep(1);
    }
    if (status == STATUS_OK) {
        winLobby->show();
        get_application()->add_window(*winLobby);
        get_application()->remove_window(*this);
        hide();
    }
}

void JoinGameWindow::notifyGameAbortOk(ClientApp* app) {
    status = STATUS_OK;
}

void JoinGameWindow::notifyGameStartOk(ClientApp* app) {
    status = STATUS_OK;
}

//{+}

